#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>

int main()
{
    pid_t id = fork();

    if(id == 0)
    {
        //子进程创建成功
        while(1)
        {
            printf("我是子进程, 我正在运行...Pid: %d\n", getpid());
            sleep(1);
            int* p = NULL;
            *p = 100;
        }
    }
    else
    {
        //父进程
        int status = 0;
        printf("我是父进程：pid:%d, 我准备电脑等待子进程啦\n", getpid());
        pid_t ret = waitpid(id, &status, 0);
        if(ret > 0)
        {
            //status >> 8 并不影响status的值
            //status >>= 8 才影响status的值
            printf("wait success, ret : %d, 我所等待子进程的退出码: %d, 退出信号是: %d\n", ret, (status >> 8) & 0xFF, status & 0x7F);
        }
    }

    return 0;
}

